#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <ctype.h>
#include <limits.h>
#include <assert.h>

struct polymorphism {
	int freq;
	int idx;
};

/* {{{ fancy Mergesort */
int
Mergesort__merge(struct polymorphism *p1, struct polymorphism *p2,
		long long unsigned *mc, const int depth,
		const int left, const int mid, const int right)
{
	struct polymorphism *array, *cpyarray;
	register int i = left, j = mid + 1, s = left;

#ifdef D
	fprintf(stderr, "mc=%llu, depth=%d, left=%d, mid=%d, right=%d\n",
			*mc, depth, left, mid, right);
#endif

	if (depth & 1)	array = p1, cpyarray = p2;
	else		array = p2, cpyarray = p1;

	while (i <= mid && j <= right) {
		if (array[i].freq <= array[j].freq) {
			cpyarray[s++] = array[i++];
		} else {
			cpyarray[s++] = array[j++];
		}
	}

	while (i <= mid)
		cpyarray[s++] = array[i++];
	while (j <= right)
		cpyarray[s++] = array[j++];

	assert(i == mid + 1);
	assert(j == right + 1);
	assert(s == right + 1);

	++*mc;
	return 0;
}

int
Mergesort__main(struct polymorphism *a, struct polymorphism *b,
		long long unsigned *mc, const int depth,
		const int L, const int R)
{
	int M = (L + R) / 2;

	if (L < R) {
		Mergesort__main(a, b, mc, depth + 1, L, M);
		Mergesort__main(a, b, mc, depth + 1, M + 1, R);
		Mergesort__merge(a, b, mc, depth, L, M, R);
	}

	return 0;
}
/* }}} */

/*
 * wrapper func
 */
int
Mergesort(struct polymorphism *array, const int n)
{
	struct polymorphism *tmp_array;
	static long long unsigned merge_count = 0;
	int depth = 0;

	tmp_array = malloc(n * sizeof *tmp_array);
	(void) memcpy(tmp_array, array, n * sizeof *array);

	Mergesort__main(array, tmp_array, &merge_count, depth, 0, n - 1);

#ifdef D
	fprintf(stderr, "merge_count=%llu\n", merge_count);
#endif

	free(tmp_array);
	return 0;
}

inline int
strstr_greedy_count(const char *haystack, const char *needle)
{
	register int count = 0;
	char *p;

	while (p = strstr(haystack, needle)) { /* clang wont be happy */
		count++;
		haystack = p + 1;
	}

	return count;
}

int
main(int argc, char **argv)
{
	static char W[11], buf[101];
	int n, i;
	char **strings;
	struct polymorphism *index;

	scanf("%s", W);
	scanf("%d", &n);
	index = calloc((size_t) n, sizeof *index);
	strings = malloc(n * sizeof *strings);

	for (i = 0; i < n; i++) {
		scanf("%s", buf);
		strings[i] = malloc(strlen(buf) + 1);
		strcpy(strings[i], buf);
		index[i].freq = strstr_greedy_count(buf, W) * -1;
		index[i].idx = i;
	}
	Mergesort(index, n);

	for (i = 0; i < n; i++)
		printf("%s\n", strings[index[i].idx]);

	for (i = 0; i < n; i++) free(strings[i]);
	free(strings);
	free(index);
	return 0;
}
